package com.aloudmeta.grpc.service;

import com.aloudmeta.grpc.demo.EchoRequest;
import com.aloudmeta.grpc.demo.EchoResponse;
import com.aloudmeta.grpc.demo.EchoServiceGrpc;
import io.grpc.stub.StreamObserver;

/**
 * huaixin 2022/1/21 2:34 PM
 */
public class EchoService extends EchoServiceGrpc.EchoServiceImplBase {

    private final String name;

    public EchoService(String name) {
        this.name = name;
    }

    @Override
    public void echo(EchoRequest request, StreamObserver<EchoResponse> responseObserver) {
        String message = request.getMessage();
        String reply = this.name + " echo: " + message;
        EchoResponse response = EchoResponse.newBuilder()
                .setMessage(reply)
                .build();
        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }
}